package com.qianfeng.smartmeeting.controller;

import com.qianfeng.smartmeeting.dto.TimeSlotDto;
import com.qianfeng.smartmeeting.entity.Room;
import com.qianfeng.smartmeeting.service.RoomService;
import com.qianfeng.smartmeeting.utils.ResponseResult;
import com.qianfeng.smartmeeting.vo.RoomVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/rooms")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @GetMapping
    public ResponseResult<List<RoomVo>> getAllRooms() {
        ResponseResult<List<Room>> result = roomService.getAllRooms();
        List<RoomVo> roomVos = result.getData().stream().map(room -> {
            RoomVo roomVo = new RoomVo();
            BeanUtils.copyProperties(room, roomVo);
            return roomVo;
        }).collect(Collectors.toList());
        
        return ResponseResult.success(roomVos);
    }

    @GetMapping("/{id}")
    public ResponseResult<RoomVo> getRoomById(@PathVariable Long id) {
        ResponseResult<Room> result = roomService.getRoomById(id);
        if (result.getCode() != 200) {
            return ResponseResult.error(result.getMessage());
        }
        
        RoomVo roomVo = new RoomVo();
        BeanUtils.copyProperties(result.getData(), roomVo);
        return ResponseResult.success(roomVo);
    }

    @GetMapping("/{id}/available-slots")
    public ResponseResult<List<TimeSlotDto>> getAvailableSlots(
            @PathVariable Long id,
            @RequestParam String date) {
        LocalDate bookingDate;
        try {
            bookingDate = LocalDate.parse(date);
        } catch (Exception e) {
            return ResponseResult.<List<TimeSlotDto>>error("日期格式不正确，请使用yyyy-MM-dd格式");
        }

        return roomService.getAvailableSlots(id, bookingDate);
    }
}